import axios from "axios";
import { ENotice } from "@/utils/tool";
import { Session } from "@/utils/storage";

const service = axios.create({
  baseURL: "http://localhost:8080",
  timeout: 5000,
});

// 请求拦截器
service.interceptors.request.use(
  (config) => {
    // 添加请求头等前置处理
    if (Session.get("token")) {
      config.headers["Authorization"] = `Bearer ${Session.get("token")}`;
    }
    return config;
  },
  (error) => {
    console.log(error);
    return Promise.reject(error);
  }
);

// 响应拦截器
service.interceptors.response.use(
  (response) => {
    // 响应后处理
    if (response.status === 200 && response.data.code === 200) {
      return Promise.resolve(response.data);
    } else {
      return Promise.reject(response.data);
    }
  },
  (error) => {
    console.log(error);
    return Promise.reject(error);
  }
);

export default service;
